/**
 * @Author yinyunqi
 * @date 2018年1月8日
 * @Content 账单
 */
package com.yongqi.controller.console;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.abel533.echarts.Option;
import com.github.pagehelper.PageHelper;
import com.yongqi.model.Family;
import com.yongqi.model.console.Bill;
import com.yongqi.service.MemberService;
import com.yongqi.service.console.BillService;
@RequestMapping("/bill")
@Controller
public class BillController {
    @Autowired
    MemberService memberService;
    @Autowired
    BillService billService;
    /**
     * 流水账单
     * @return listBill.html
     */
    @RequestMapping("/listBill")
    public String listBill(Model model) {
	String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
	List<String> memberNametList = memberService.selectMemberByFamily(familyAccount);
	model.addAttribute("nameList", memberNametList);
	return "bill/ListBill";
    }
    /**
     * 分类账单
     * @return cateBill.html
     */
    @RequestMapping("/cateBill")
    public String cateBill(Model model) {
	String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
	List<String> memberNametList = memberService.selectMemberByFamily(familyAccount);
	model.addAttribute("nameList", memberNametList);
	return "bill/CateBill";
    }
    /**
     * 账单分页查询
     * @param request
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/query")
    public String queryItems(HttpServletRequest request,@RequestParam int pageNumber ,int pageSize) {
	String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
	Map<String, String> map = new HashMap<String, String>();
	map.put("familyAccount", familyAccount);
	int total = billService.queryBill(map).size();
	PageHelper.startPage(pageNumber,pageSize);
	List<Bill> incomeList = billService.queryBill(map);
	JSONObject object = new JSONObject();
	object.put("rows", incomeList);
	object.put("total", total);
	return object.toJSONString();
    }
    /**
     * 账单条件查询
     * @param request
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/conQuery")
    public String conditionQuery(HttpServletRequest request,@RequestParam int pageNumber ,int pageSize,String item,String member,String date,String dateEnd) {
	HashMap<String, String> conMap = new HashMap<String,String>();
	conMap.put("item", item);
	conMap.put("member", member);
	conMap.put("date", date);
	conMap.put("dateEnd", dateEnd);
	int total = billService.queryBillByCondition(conMap).size();
	PageHelper.startPage(pageNumber,pageSize);
	List<Bill> incomeList = billService.queryBillByCondition(conMap);
	System.out.println(total);
	JSONObject object = new JSONObject();
	object.put("rows", incomeList);
	object.put("total", total);
	return object.toJSONString();
    }
    @ResponseBody
    @RequestMapping("/cateQuery")
    public String cateQuery() {
	System.out.println("cateQuery");
	Option option = billService.Echart();
	JSONObject object = new JSONObject();
	object.put("option", option);
	return object.toJSONString();
    }
    /**
     * 展示月支出的饼图
     * @return
     */
    @ResponseBody
    @RequestMapping("/payPie")
    public String payPie(HttpServletRequest request) {
	String time = request.getParameter("time");
	String member = request.getParameter("member");
	System.out.println("payPie");
	Option option = billService.payPie(time,member);
	JSONObject object = new JSONObject();
	object.put("option", option);
	return object.toJSONString();
    }
    /**
     * 展示月收入的饼图
     * @return
     */
    @ResponseBody
    @RequestMapping("/incomePie")
    public String incomePie(HttpServletRequest request) {
	String time = request.getParameter("time");
	String member = request.getParameter("member");
	System.out.println("incomePie");
	Option option = billService.incomePie(time,member);
	JSONObject object = new JSONObject();
	object.put("option", option);
	return object.toJSONString();
    }
    /**
     * 展示月借款的饼图
     * @return
     */
    @ResponseBody
    @RequestMapping("/debtPie")
    public String debtPie(HttpServletRequest request) {
	String time = request.getParameter("time");
	String member = request.getParameter("member");
	System.out.println("debtPie");
	Option option = billService.debtPie(time,member);
	JSONObject object = new JSONObject();
	object.put("option", option);
	return object.toJSONString();
    }
    /**
     * 展示月统计的柱状图
     * @return
     */
    @ResponseBody
    @RequestMapping("/totalBar")
    public String totalBar(HttpServletRequest request) {
	String time = request.getParameter("time");
	System.out.println("totalBar");
	Option option = billService.totalBar(time);
	JSONObject object = new JSONObject();
	object.put("option", option);
//	String newOption = option.toString().replaceAll("@", "\",\"");
//	System.out.println(newOption);
//	object.put("option2", newOption);
	return object.toJSONString().replaceAll("@","\",\"");
    }
}

